import router from '@/routers';
import { allowList, loginRoutePath, STORAGE_TOKEN_KEY } from '@/types/meta';

router.beforeEach((to) => {
	const userToken = localStorage.getItem(STORAGE_TOKEN_KEY);
	if (!userToken) {
		// 白名单路由列表检查
		if (allowList.includes(to.name as string)) {
			return true;
		}
		if (to.fullPath !== loginRoutePath) {
			// 未登录，进入到登录页
			return {
				path: loginRoutePath,
				replace: true,
			};
		}
		return to;
	}
});
